package com.blt.govdata.controller;

import com.blt.govdata.controller.base.BaseController;
import com.blt.govdata.core.constants.Constants;
import com.blt.govdata.model.TSysOrganization;
import com.blt.govdata.model.vo.TSysOrganizationVo;
import com.blt.govdata.service.ITSysOrganizationService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 部门管理前端控制器
 * </p>
 * @author AustinGJ123
 * @since 2018-03-26
 */
@RestController
@RequestMapping("/tSysOrganization")
//todo 将所有的XXX修改为真实值
public class TSysOrganizationController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ITSysOrganizationService service;

    @PostMapping("/getOrgs")
    public Map<String, Object> getOrgs(@RequestParam Map<String, Object> params) {
        params.put("Pid", 0);
        List<TSysOrganizationVo> orgs = this.service.selectVoOrgsByPid(params);
        Iterator it = orgs.iterator();
        while (it.hasNext()) {
            TSysOrganizationVo org = (TSysOrganizationVo) it.next();
            if (org != null && org.getHasChild() == 1) {
                params.put("Pid", org.getId());
                org.setChildren(this.service.selectVoOrgsByPid(params));
            }
        }
        return ImmutableMap.of("status", "true", "data", orgs);
    }

    /**
     * 递归查询部门的方法 (最好别用递归，容易造成死循环)
     * */
//    private List<TSysOrganizationVo> getOrgChildren(List<TSysOrganization> list){
//       Iterator it  = list.iterator();
//        List<TSysOrganizationVo> voList = Lists.newArrayList();
//       while (it.hasNext()){
//           TSysOrganization po = (TSysOrganization)it.next();
//           TSysOrganizationVo vo = new TSysOrganizationVo();
//           BeanUtils.copyProperties(po,vo);
//           if(vo.getHasChild() == 1) vo.setChildren(this.service.selectList(new EntityWrapper<TSysOrganizationVo>().where("P_Id = 0",vo.getId())));
//       }
//    }
//

    /**
     * 添加部门  (添加根节点，添加子节点)注意P_Id
     */
    @PostMapping("/addOrg")

    public Map<String, Object> addOrg(TSysOrganization sysOrganization) {
        if (null == sysOrganization) return ImmutableMap.of("status", "true", "data", false);
        Integer Pid = sysOrganization.getpId();
        if (null == Pid) sysOrganization.setpId(0);
        if (null != Pid && Pid != 0) {
            TSysOrganization orgParent = new TSysOrganization();
            orgParent.setId(Pid);
            orgParent.setHasChild(1);
            this.service.updateById(orgParent);
        }
        boolean result = this.service.insert(sysOrganization);
        logger.info("添加部门");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 编辑部门
     */
    @PostMapping("/editOrg")

    public Map<String, Object> editOrg(TSysOrganization sysOrganization) {
        if (null == sysOrganization || sysOrganization.getId() == null)
            return ImmutableMap.of("status", "true", "data", false);
        boolean result = this.service.updateById(sysOrganization);
        logger.info("编辑部门");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 删除部门
     */
    @PostMapping("/deleteOrg")

    public Map<String, Object> deleteOrg(TSysOrganization sysOrganization) {
        if (null == sysOrganization || sysOrganization.getId() == null)
            return ImmutableMap.of("status", "true", "data", false);
        sysOrganization.setIsDelete(Constants.IS_DELETED);
        boolean result = this.service.updateById(sysOrganization);
        logger.info("逻辑删除部门");
        return ImmutableMap.of("status", "true", "data", result);
    }

    /**
     * 根据Id查询部门
     */
    @RequestMapping(value = "selectOrgById", params = {"id"})
    public Map<String, Object> selectOrgById(Integer id) {
        return ImmutableMap.of("status", "true", "data", this.service.selectById(id));
    }

}
